package com.controller;

import com.entity.Insurance;
import com.service.InsuranceService;
import com.service.UserService;
import com.utils.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.InitializingBean;
import com.entity.Orders;
import com.service.OrdersService;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

import com.entity.Result;
import com.utils.ResultUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import com.entity.User;

import java.util.Date;

import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping(value = "/admin/orders")
public class AdminOrdersController extends BaseController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private InsuranceService insuranceService;
    @Autowired
    private UserService userService;


    @RequestMapping("add")
    public String add(HttpServletRequest request, Orders model) {
        User user = (User) request.getSession().getAttribute("user");
        List<Insurance> insuranceList = insuranceService.getModelList(MapUtil.buildMap("userId", user.getId()));
        request.setAttribute("model", model);
        request.setAttribute("insuranceList", insuranceList);
        return "/admin/orders-add";
    }


    @RequestMapping("edit")
    public String edit(HttpServletRequest request, String id) {
        Orders model = ordersService.getModel(id);
        request.setAttribute("model", model);
        return "/admin/orders-add";
    }

    @RequestMapping("save")
    @ResponseBody
    public Result save(HttpServletRequest request, Orders model) {
        User user = (User) request.getSession().getAttribute("user");
        int i = 0;
        if (StringUtils.isBlank(model.getId())) {
            model.preInsert();
            model.setCreateDate(new Date());
            i = ordersService.insertModel(model);
        } else {
            i = ordersService.updateModel(model);
        }
        if (i == 0) {
            return ResultUtil.error("失败");
        } else {
            return ResultUtil.success(null);
        }

    }

    @RequestMapping("approved")
    @ResponseBody
    public Result approved(String id,Integer state){
        Orders model = ordersService.getModel(id);
        model.setState(state);
        if (state == 5){
            getMoney(id);
        }
        int i = ordersService.updateModel(model);
        if (i == 0) {
            return ResultUtil.error("失败");
        } else {
            return ResultUtil.success(null);
        }
    }

    /**
     * 发放赔偿金
     */
    public void getMoney(String orderId){
        Orders orders = ordersService.getModel(orderId);//获取保单
        User user = userService.getModel(orders.getUserId());//获取用户
        BigDecimal price = orders.getPrice();//保单申请金额
        user.setMoney(user.getMoney().add(price));//用户账户余额增加保费
        userService.updateModel(user);
    }


    //删除批量删除
    @RequestMapping("delete")
    @ResponseBody
    public Result delete(HttpServletRequest request, String id) {

        if (StringUtils.isNotBlank(id)) {
            String[] split = id.split(",");
            for (int i = 0; i < split.length; i++) {
                ordersService.deleteModel(split[i]);
            }
            return ResultUtil.success();
        } else {
            return ResultUtil.error("未选中删除项！");
        }

    }


    @RequestMapping("list")
    public String list(HttpServletRequest request, Orders model) {

        request.setAttribute("model", model);
        return "/admin/orders-list";
    }

    /**
     * 用户提交的申请理赔
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("approve")
    public String approve(HttpServletRequest request, Orders model) {

        request.setAttribute("model", model);
        return "/admin/orders-approve";
    }


    /**
     * 已经有调查勘察记录的申请单，确认是否发放赔偿金
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("money/give")
    public String moneyGive(HttpServletRequest request, Orders model) {

        request.setAttribute("model", model);
        return "/admin/orders-money";
    }

    /**
     * 用户提交的需要调查的
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("investigation")
    public String investigation(HttpServletRequest request, Orders model) {

        request.setAttribute("model", model);
        return "/admin/orders-investigation";
    }
    /**
     * 用户提交的需要调查的
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("responsibility")
    public String responsibility(HttpServletRequest request, Orders model) {

        request.setAttribute("model", model);
        return "/admin/orders-responsibility";
    }



    //获取列表数据
    @RequestMapping("list/data")
    @ResponseBody
    public Result list(HttpServletRequest request, Orders model, @RequestParam(defaultValue = "1") Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Orders> list = ordersService.getModelList(putEntityInMap(model));
        PageInfo<Orders> pageInfo = new PageInfo<>(list);
        return ResultUtil.success(list, pageInfo.getTotal());
    }


}